#include "GlobalDefs.h"
#include "DataConvert.h"


void VariantToBinary(const QVariant &var, string &result)
{
    DataConvert::toBinary(var, result);
}

void BinaryToVariant(const string &data, QVariant &result)
{
    DataConvert::fromBinary(data, result);
}

void QStringToStdWString(const QString &s, std::wstring &result)
{
    result.resize(s.length());
    if (sizeof(wchar_t) == sizeof(ushort)) {
        memcpy(&result[0], s.utf16(), sizeof(wchar_t)*s.length());
    }
    else {
        wchar_t *a = &result[0];
        const unsigned short *uc = s.utf16();
        for (int i = 0; i < s.length(); ++i) {
            uint u = uc[i];
            if (u >= 0xd800 && u < 0xdc00 && i < s.length()-1) {
                ushort low = uc[i+1];
                if (low >= 0xdc00 && low < 0xe000) {
                    ++i;
                    u = (u - 0xd800)*0x400 + (low - 0xdc00) + 0x10000;
                }
            }
            *a = wchar_t(u);
            ++a;
        }
    }
}

void StdWStringToQString(const std::wstring &s, QString &result)
{
    if (sizeof(wchar_t) == sizeof(ushort)) {
        result = QString::fromUtf16((ushort *)s.c_str(), s.size());
    }
    else {
        result = QString::fromUcs4((uint *)s.c_str(), s.size());
    }
}
